// 定义懒加载指令
import { useIntersectionObserver } from '@vueuse/core'

export const lazyPlugin = {
    install(app:any){
        // 懒加载指令
        app.directive('img-lazy',{
            mounted(el:any,binding:any){
                // 当图片进入视口时，isIntersecting会变为true
                const {stop} = useIntersectionObserver(
                    el,
                    ([{ isIntersecting }]) => {
                        if(isIntersecting){ 
                            el.src = binding.value
                            stop()
                        }
                    },
                ) 
            }
        })
    }
}